$(function () {

  // axios.defaults.baseURL = "http://nq2dbary.dongtaiyuming.net/";
  // 轮播图
  var baseURL = 'http://nq2dbary.dongtaiyuming.net/'
  // var baseURL = 'http://localhost:8888/'
  // var form = layui.form
  var layer = layui.layer


  // 获取轮播图列表数据
  function loadSwiperList() {
    $.ajax({
      type: 'get',
      url: baseURL + 'admin/swipers',
      headers: {
        "Authorization": sessionStorage.getItem("token")
      },
      success: function (res) {
        // console.log(res);
        //template模板
        var tags = template('table-tpl', res)

        $('.layui-table tbody').html(tags)
      }
    })
  }
  loadSwiperList()
  //轮播图状态切换/admin/swipers/:id
  $(".layui-table tbody").on("click", ".layui-badge", function (e) {
    // console.log(123);
    // console.log( $(e.target).data('id'));
    // console.log( $(e.target).data);
    let id = $(e.target).data('id')
    let sta = $(e.target).data('status')
    // console.log($(e.target).data('status'));
    $.ajax({
      type: 'put',
      url: baseURL + 'admin/swipers/' + id,
      data: {
        status: sta,
      },
      headers: {
        "Authorization": sessionStorage.getItem("token")
      },
      success: function (res) {
        // console.log(res);
        if (res.status === 0) {
          layer.msg(res.message)
          loadSwiperList()
        }
        // loadSwiperList()
      }
    })

  })

  //批量上传图片
  $("#uploadSwiper").on("click", function () {
    // ev.preventDefault();
    // console.log(123);
    // $("#myfile").attr('style', 'display: bolock;')
    $("#myfile").click();

  })
  $("#myfile").change(function (ev) {
    let files = ev.target.files;
    var fd = new FormData();
    $.each(files, function (index, values) {
      fd.append('swipers', values)
    })


    $.ajax({
      type: 'post',
      url: baseURL + 'admin/swipers',
      data: fd,
      processData: false,
      contentType: false,
      headers: {
        "Authorization": sessionStorage.getItem("token")
      },
      success: function (res) {
        // console.log(res);
        if (res.status === 0) {
          layer.msg(res.message)
          loadSwiperList()
        }
      }
    })
  })

  //删除图片
  $(".layui-table tbody").on('click', ".delete", function (e) {
    // console.log(123);
    let id = $(e.target).data('id')
    $.ajax({
      type: 'DELETE',
      url: baseURL + 'admin/swipers/' + id,
      headers: {
        "Authorization": sessionStorage.getItem("token")
      },
      success: function (res) {
        // console.log(res);
        if (res.status === 0) {
          layer.msg(res.message) >>>

            loadSwiperList()
        }
      }
    })
  })
})